


#include <structure.h>
#ifdef INHERIT_FROM_MODEL_MOUNTSTREAM
#ifdef strType
#ifdef DEFAULT_CONTAINER_TYPE


#include "model_mountStream_IO.h"




#define u32  unsigned int
#define u16  unsigned short
#define u8   unsigned char

#include "length.h"
//#define _length  unsigned int
//_length is basic type to measure the length or offset, it's customized -- but you should give a way to compare
//in the length.h there is a type __LENGTH used as the specfic size


#define UNLIMITED 0
//ONLY used in num/size

#include "OSProc.h"


//using CPP -- the redifinated func
#define _STRFUNC(funcName)              funcName
#define STRFUNC(funcName) 	        _STRFUNC(funcName)

#define _STRFUNC_(funcName)              strType##_##funcName
#define STRFUNC_(funcName)		_STRFUNC_(funcName)

#define __BlkState(stream)  ((stream)->state)

#define __DEFINED_STREAM_STRUCT_NAME(x,y) x##_##y
#define _DEFINED_STREAM_STRUCT_NAME  __DEFINE_STREAM_STRUCT_NAME(strType,FULLNAME)
#define _SYSTEM_PROTECT  1

/*
struct _DEFINED_STREAM_STRUCT_NAME
{
  unsigned int state;
  DEFAULT_CONTAINER_TYPE container;
  char name[MOUNTNAME_LEN];
  //  _pcb* protecter;
  INHERIT_FROM_MODEL_MOUNTSTREAM
};

typedef struct _DEFINED_STREAM_STRUCT_NAME*   strType;
*/
#include "struct_model_mountStream.h"



strType STRFUNC(CreateStream)(strType (*allocMemory)(_ptlong))
{
  strType res = allocMemory(sizeof(struct _DEFINED_STREAM_STRUCT_NAME));
  //Memall(0);...
  //res->state = 0;
#ifdef _InitMountStream
  _InitMountStream(res);
#endif
  return res;	
}


static strType STRFUNC(CreateStreamBlock)(strType stream)
{
  return (strType)NULL;
}

//not Inherit from model_Stream_IO.h

//type-check
//Convert is always in priority


_ptlong STRFUNC(ConvertToPub) (strType from, _dfps to, _length num, _length offset)
{
  return _ConvertToPub(from,to,num,offset);
}

_ptlong STRFUNC(ConvertToPrv) (_dfps from, strType to, _length num, _length offset)
{
  return _ConvertToPrv(from,to,num,offset);
}

strType STRFUNC(WriteStream) (strType stream, u8* input, _length num, _length offset)
{
  return _WriteStream(stream,input,num,offset);
}

strType STRFUNC(ReadStream)  (strType stream, u8* output,  _length num, _length offset)
{
  return _ReadStream(stream,output,num,offset);
}

strType STRFUNC(PeekStream)  (strType stream, u32* output,  _length num, _length offset)
{
  return _PeekStream(stream,output,num,offset);
}

strType STRFUNC(SeekStream)  (strType stream,  _length num)
{
  return _SeekStream(stream,num);
}


_ptlong STRFUNC(CombineStream)(strType to, strType from)
{
  return _CombineStream(to,from);
}

_ptlong STRFUNC(ApartStream)(strType from, strType to, _length partpoint)
{
  return _ApartStream(from, to, partpoint);
}

_dfps STRFUNC(IOStream)(_dfps from,strType to,_length num,_length offset)
{
  return _IOStream(from,to,num,offset);
}

_length STRFUNC(GetLengthStream)(strType of)
{
  return _GetLengthStream(of);
}



_ptlong STRFUNC(EndStream)(strType target)
{
  if(target->container != NULL)
    UnmountStream(target,ROOT);
  _EndStream(target);
  return _KHEAPRELEA(target);
}



#define __ConnectStreamToThread(fromStream,tth)	do\
{							\
	slnode* newnode = _KHEAPAPPLY(sizeof(slnode));	\
	MakeSListNode(newnode,fromStream);		\
	if(tth->streams == NULL)			\
		tth->streams = newnode;			\
	else						\
	{						\
		newnode->next = tth->streams->next;	\
		tth->streams->next = newnode;		\
	}						\
}while(0)


strType STRFUNC(AddStreamTo) (strType stream, _tcb* tth)
{
	unsigned int state = __BlkState(stream);
	//	if((state & IS_STREAM) && (ALL_READING(state) == 1))
	// return(strType A);
	((ALL_READING_ADD(stream) == 1)?0:(return (strType A)));
	//	((NOW_READING_ADD(stream) == 1)?0:(return (strType A)));
	__ConnectStreamToThread(stream,tth);
	return stream;
}


#endif
#endif
#endif
